package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/10 21:59
 * https://leetcode-cn.com/problems/solve-the-equation/
 */
public class Title640 {

    public static String solveEquation(String equation) {
        int sum = 0, numX = 0, flag = 1;
        for (String s : equation.split("=")) {
            String replace = s.replace("-", "+-");
            for (String temp: replace.split("\\+")) {
                if ("".equals(temp)) {
                    continue;
                }
                // x 2x -x
                if (temp.contains("x")) {
                    if (temp.length() == 1) {
                        numX += flag;
                    }else if(temp.length()==2 && temp.charAt(0) == '-') {
                        numX += -flag;
                    }else{
                        numX += Integer.valueOf(temp.substring(0, temp.length() - 1)) * flag;
                    }
                }else{
                    //数字 -5 10
                    sum += flag * Integer.valueOf(temp);
                }
            }
            flag = -1;
        }
        if (numX == 0 && sum == 0) {
            return "Infinite solutions";
        } else if (numX == 0 && sum != 0) {
            return "No solution";
        } else {
            sum = -sum / numX;
            return "x=" + sum;
        }
    }

    public static  void main(String[] args) {
        String equation = "x+5-3+x=6+x-2";
        String s = solveEquation(equation);
        System.out.println(s);
    }
}
